package com.shangqing.document_management.handler;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.github.pagehelper.util.StringUtil;
import com.shangqing.document_management.entity.User;
import com.shangqing.document_management.jwt.Token;
import com.shangqing.document_management.jwt.UserLoginToken;
import com.shangqing.document_management.service.AdminService;
import com.shangqing.document_management.constant.Result;
import com.shangqing.document_management.util.MD5Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSONObject;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

@RequestMapping("/user")
@RestController
public class AdminHandler {
    @Autowired
    AdminService adminService;
    private Logger logger = LoggerFactory.getLogger(AdminHandler.class);

    /**
     * 用户登录
     * @param username
     * @param password
     * @param session
     * @return
     */
    @PostMapping("/login.html")
    public Object Login(@RequestParam("userName") String username,
                              @RequestParam("password") String password,
                              HttpSession session){
        JSONObject jsonObject=new JSONObject();
        logger.debug(username+"====="+password);
        if(!("").equals(username)&&!("").equals(password)){
            User user=adminService.selectUserByName(username);
            if(user!=null){
                logger.info(username+"用户de密码为"+user.getPassword());
                 if(user.getPassword().equals(MD5Util.md5Password(password))){
                     logger.info(username+"登录成功");
                     Token token=new Token();
                     token.getToken(user);
                     logger.info(token.getToken(user));
                     logger.info("id"+user.getId()+"username"+user.getUsername()+"password"+user.getPassword()+"idcard"+user.getIdcard()+"phone"+user.getPhone()+"roleid"+user.getRoleid());
                     jsonObject.put("token",token.getToken(user));
                     jsonObject.put("usre",user);
                     return jsonObject;
                 }
                 jsonObject.put("meaages","密码输入错误");
            }
            jsonObject.put("meaages","用户不存在");
            return jsonObject;
        }
        jsonObject.put("meaages","用户名或密码不能为空");
        return jsonObject;

    }

    /**
     * 用户注销
     * @param session
     * @return
     */
    @RequestMapping("/logout.html")
    public Result<User> Logout(HttpSession session){
        session.invalidate();
        return new Result<User>(200,"退出成功","Logout",null);
    }

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @return
     */
    @UserLoginToken
    @GetMapping("/userpage")
    public Result<User> UserPage(@RequestParam("pageNum") String pageNum,
                                 @RequestParam("pageSize") String pageSize){
        logger.info("进入分页，当前页为"+pageNum+"页大小为"+pageSize);
        Integer currPage;
        Integer currPageSize;
        if(StringUtil.isEmpty(pageNum)){
            currPage=1;
        }else{
            currPage=Integer.parseInt(pageNum);
        }
        if(StringUtil.isEmpty(pageSize)){
            currPageSize=10;
        }else{
            currPageSize=Integer.parseInt(pageSize);
        }
       List<User> list=adminService.findAll(currPage,currPageSize).getList();

        return new Result(200,"分页查询","UserPage",list);
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @PostMapping("/adduser")
    public Result<User> AddUser(@RequestBody @Valid User user, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            logger.info("用户校验不通过");
            return new Result<>(400,"用户校验不通过,用户名，密码，身份证，手机号不能为空");
            };

        System.out.println("进入"+user.getUsername()+"============================");

       String password=MD5Util.md5Password(user.getPassword());
       user.setPassword(password);
        Boolean flag =adminService.addUser(user);
        if(flag==true){
            return new Result<User>(200,"用户插入成功","AddUser",user);
        }else{
            return new Result<User>(400,"用户插入失败","AddUser");
        }
    }
}
